package com.imodia.checkoo.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.hsq.commons.util.StringUtil;
import com.hsq.commons.util.db.PageBean;
import com.hsq.commons.util.db.SqlParam;
import com.hsq.commons.util.html.BaseUrl;
import com.hsq.commons.util.tree.TreeNodeBean;
import com.imodia.checkoo.bean.CmsArticleCatalogueBean;
import com.imodia.checkoo.bean.CmsCatalogueBean;
import com.imodia.checkoo.bean.CmsDataDictionaryBean;
import com.imodia.checkoo.bean.CmsDocumentBean;
import com.imodia.checkoo.bean.CmsImageSourceBean;
import com.imodia.checkoo.bean.CmsModelBean;
import com.imodia.checkoo.bean.CmsPageBean;
import com.imodia.checkoo.bean.CmsPageRefColumnsBean;
import com.imodia.checkoo.bean.CmsSiteBean;
import com.imodia.checkoo.service.CmsArticleCatalogueService;
import com.imodia.checkoo.service.CmsCatalogueService;
import com.imodia.checkoo.service.CmsDataDictionaryService;
import com.imodia.checkoo.service.CmsDocumentService;
import com.imodia.checkoo.service.CmsImageSourceService;
import com.imodia.checkoo.service.CmsModelService;
import com.imodia.checkoo.service.CmsPageRefColumnsService;
import com.imodia.checkoo.service.CmsPageService;
import com.imodia.checkoo.service.CmsSiteService;

@Controller
@RequestMapping("/hfmanager/catalogue/list.do")
public class CmsCatalogueController {
	
	private final Log log = LogFactory.getLog(CmsDocumentController.class);

	private final String baseUrl = "catalogue";

	private final String url_list = "/hfmanager/" + baseUrl + "/" + baseUrl+ "_list";
	private final String url_redirect = "/hfmanager/" + baseUrl+ "/list.do?action=edit";
	private final String url_edit = "/hfmanager/" + baseUrl + "/" + baseUrl+ "_edit";	
	private final String url_list4page = "/hfmanager/" + baseUrl + "/" + baseUrl+ "_list4page";	
	private final String url_list4article = "/hfmanager/" + baseUrl + "/" + baseUrl+ "_list4article";
	private final String url_eidt4page_list = "/hfmanager/" + baseUrl + "/" + baseUrl+ "_eidt4page_list";	
	private final String url_listredirect = "/hfmanager/" + baseUrl+ "/list.do?action=list";
	private final String url_delete4page_list = "/hfmanager/" + baseUrl + "/" + baseUrl+ "_delete4page_list";
	

	@Autowired
	private CmsCatalogueService cmsCatalogueService;
	@Autowired
	private CmsModelService cmsModelService;
	@Autowired
	private CmsPageRefColumnsService cmsPageRefColumnsService;
	@Autowired
	private CmsArticleCatalogueService cmsArticleCatalogueService;
	@Autowired
	private CmsImageSourceService cmsImageSourceService;
	@Autowired
	private CmsPageService cmsPageService;
	@Autowired
	private CmsDocumentService cmsDocumentService;
	@Autowired
	private CmsDataDictionaryService cmsDataDictionaryService;
	@Autowired
	private CmsSiteService cmsSiteService;
	

	

	@RequestMapping(params = "action=delete")
	public ModelAndView delete(HttpServletRequest request) {
		int id = StringUtil.requestInt(request, "id");
		int checked = StringUtil.requestInt(request, "checked");
		
		List<CmsPageBean> pageList = cmsPageService.getByCataId(id);
		CmsCatalogueBean cata = cmsCatalogueService.get(id);
		if(pageList.size()>0 && checked==0){
			Map<String, Object> m = new HashMap<String, Object>();
			m.put("pageList", pageList);
			
			m.put("cata", cata);
			return new ModelAndView(url_delete4page_list, m);
		}
		if(cata!=null && cata.getId()>0){
			 cmsModelService.delete(cata.getRsId(),cata.getId());
		}
		cmsCatalogueService.deleteAll(id);
		
		int documentId = StringUtil.requestInt(request, "documentId");
		
		return new ModelAndView(new RedirectView(request.getContextPath()
				+ url_listredirect+"&documentId="+documentId), null);
	}

	@RequestMapping(params = "action=save")
	public ModelAndView edit(
			@ModelAttribute("userMessage") CmsCatalogueBean obj, HttpServletRequest request) {
		int x = 0;
		if(obj.getId()!=null && obj.getId()>0){
			x = cmsCatalogueService.update(obj);
		}else{
			x = cmsCatalogueService.insert(obj);
			obj.setId(x);
		}
		if(x>0){
			x=1;
		}
		return new ModelAndView(new RedirectView(request.getContextPath()+ url_redirect+"&documentId="+obj.getDocumentId()+"&id="+obj.getId()+"&x="+x), null);
	}

	@RequestMapping(params = "action=edit")
	public ModelAndView edit_in(@ModelAttribute("userMessage") CmsCatalogueBean obj,HttpServletRequest request) {
		int id = StringUtil.requestInt(request, "id");
		Map<String, Object> m = new HashMap<String, Object>();
		CmsCatalogueBean info = cmsCatalogueService.get(id);
		if (info == null)
			info = obj;
		m.put("info", info);
		
		if(info.getId()!=null && info.getId()>0){
			SqlParam modelSql = new SqlParam();
			modelSql.andEqualTo("ref_rs_id", String.valueOf(info.getRsId()));
			modelSql.andEqualTo("ref_id",  String.valueOf(info.getId()));
			List<CmsModelBean> modelList = cmsModelService.list(modelSql);
			m.put("modelList", modelList);
		}
		
		List<TreeNodeBean> parentlist = cmsCatalogueService.getTreePage2(info.getSiteId(),4,0,0);
		m.put("parent", parentlist);
		
		
		m.put("site", cmsSiteService.list(new SqlParam()));
		
		List<CmsDocumentBean> document = cmsDocumentService.list();
		m.put("document", document);
		
		//��ʾ����
		List<CmsDataDictionaryBean> showTypeList = cmsDataDictionaryService.getClildInfo(4);
		m.put("showTypeList", showTypeList);
		
		return new ModelAndView(url_edit, m);
	}

	@RequestMapping(params = "action=list")
	public ModelAndView list(HttpServletRequest request) {
		HashMap<String, Object> model = new HashMap<String, Object>();
		int siteId = StringUtil.requestInt(request, "siteId",1);
		int documentId = StringUtil.requestInt(request, "documentId",0);
		int showType = StringUtil.requestInt(request, "showType",0);

		model.put("siteId", siteId);
		
		List<TreeNodeBean> list = cmsCatalogueService.getTreePage2(siteId,4,documentId,showType);
		
		model.put("info", list);
		
		model.put("siteInfo", cmsSiteService.list(new SqlParam()));
		
		List<CmsDocumentBean> document=cmsDocumentService.list();
		model.put("document", document);
		
		//��ʾ����
		List<CmsDataDictionaryBean> showTypeList = cmsDataDictionaryService.getClildInfo(4);
		model.put("showTypeList", showTypeList);
		
		return new ModelAndView(url_list, model);
	}
	
	
	@RequestMapping(params = "action=list4article")
	public ModelAndView list4articel(HttpServletRequest request, CmsCatalogueBean obj,
			PageBean page) {
		HashMap<String, Object> model = new HashMap<String, Object>();
		int siteId = StringUtil.requestInt(request, "siteId");
		List<TreeNodeBean> list = cmsCatalogueService.getTreePage2(siteId,4,0,0);
		model.put("info", list);
		
		int articleId = StringUtil.requestInt(request, "articleId");
		SqlParam param = new SqlParam();
		param.andEqualTo("article_id", String.valueOf(articleId));
		List<CmsArticleCatalogueBean> articleCatalogueList = cmsArticleCatalogueService.list(param);
		for(int i=0;i<list.size();i++){
			TreeNodeBean tree = list.get(i);
			tree.setObj(null);
			for(int j=0;j<articleCatalogueList.size();j++){
				int nodeId = Integer.parseInt(tree.getNodeId());
				CmsArticleCatalogueBean o = articleCatalogueList.get(j);
				if(o.getCatalogueId()==nodeId){
					tree.setIsSelected(1);
					tree.setObj(o);
				}
			}
		}
		
		List<CmsImageSourceBean> imgList = cmsImageSourceService.list(param);
		model.put("imgList", imgList);


		
		model.put("siteInfo", cmsSiteService.list(new SqlParam()));
		
		
		return new ModelAndView(url_list4article, model);
	}
	
	
	@RequestMapping(params = "action=list4page")
	public ModelAndView list4page(HttpServletRequest request, CmsCatalogueBean obj,
			PageBean page) {
		HashMap<String, Object> model = new HashMap<String, Object>();
		int siteId = StringUtil.requestInt(request, "siteId");
		int pageId = StringUtil.requestInt(request, "pageId");
		
		List<TreeNodeBean> list = cmsCatalogueService.getTreePage2(siteId,4,0,0);
		
		List<CmsPageRefColumnsBean> pageRefColumns = cmsPageRefColumnsService.listByPage(pageId);
		for(int i=0;i<list.size();i++){
			TreeNodeBean tree = list.get(i);
			for(int j=0;j<pageRefColumns.size();j++){
				CmsPageRefColumnsBean pageRef =  pageRefColumns.get(j);
				int nodeId = Integer.parseInt(tree.getNodeId());
				if(nodeId==pageRef.getCatalogueId()){
					tree.setIsSelected(1);
					CmsCatalogueBean catalogue = (CmsCatalogueBean)tree.getObj();
					if(catalogue!=null){
						catalogue.setPageId(pageId);
						catalogue.setCatalogueId(pageRef.getCatalogueId());
						catalogue.setHorizontalOrder(pageRef.getHorizontalOrder());
						catalogue.setRowOrder(pageRef.getRowOrder());
						catalogue.setOrderNum(pageRef.getOrderNum());
					}
				}
			}
		}
		model.put("info", list);
		
		model.put("site", cmsSiteService.get(siteId));
		
		CmsPageBean pageInfo = cmsPageService.get(pageId);
		model.put("pageInfo", pageInfo);
		
		List<CmsDocumentBean> document=cmsDocumentService.list();
		model.put("document", document);
		
		return new ModelAndView(url_list4page, model);
	}
	
	
	@RequestMapping(params = "action=eidt4page_list")
	public ModelAndView eidt4page_list(HttpServletRequest request, CmsCatalogueBean obj,PageBean page) {
		HashMap<String, Object> model = new HashMap<String, Object>();
		int siteId = StringUtil.requestInt(request, "siteId");
		int catalogueId = StringUtil.requestInt(request, "catalogueId");
		int pageId = StringUtil.requestInt(request, "pageId");
	
		List<TreeNodeBean> list = cmsPageService.getTreePage2(siteId,0,0);
		model.put("info", list);
		
		CmsSiteBean site = cmsSiteService.get(siteId);
		model.put("site", site);
		
		CmsCatalogueBean catalogue = cmsCatalogueService.get(catalogueId);
		model.put("catalogue", catalogue);

		SqlParam sql = new SqlParam();
		sql.andEqualTo("catalogue_id", request.getParameter("catalogueId"));
		List<CmsPageRefColumnsBean> pageRefColumns = cmsPageRefColumnsService.list(sql);
		for(int i=0;i<list.size();i++){
			TreeNodeBean tree = list.get(i);
			for(int j=0;j<pageRefColumns.size();j++){
				CmsPageRefColumnsBean pageRef =  pageRefColumns.get(j);
				int nodeId = Integer.parseInt(tree.getNodeId());
				if(nodeId==pageRef.getPageId()){
					tree.setIsSelected(1);
					tree.setObj(pageRef);
				}
			}
		}
		
		CmsPageBean pageInfo = cmsPageService.get(pageId);
		model.put("pageInfo", pageInfo);
		
		return new ModelAndView(url_eidt4page_list, model);
	}
	
	
	
	@RequestMapping(params = "action=saveArticleRefCatalogue")
	public ModelAndView saveArticleRefCatalogue( HttpServletRequest request) {
		int articleId = StringUtil.requestInt(request, "articleId");
		String[] catalogueId = request.getParameter("catalogueId").split(",");
		String[] listOrder = request.getParameter("listOrder").split(",");
		String[] imgId = request.getParameter("imgId").split(",");

		List<CmsArticleCatalogueBean>  list = new ArrayList<CmsArticleCatalogueBean>();
		for(int i=0;i<listOrder.length;i++){
			CmsArticleCatalogueBean pc = new CmsArticleCatalogueBean();
			pc.setArticleId(articleId);
			if(catalogueId[i].matches("\\d+"))
				pc.setCatalogueId(Integer.parseInt(catalogueId[i]));
			if(listOrder[i].matches("\\d+"))
				pc.setListOrder(Integer.parseInt(listOrder[i]));
			if(imgId[i].matches("\\d+"))
				pc.setImgId(Integer.parseInt(imgId[i]));
			list.add(pc);
		}
		cmsArticleCatalogueService.saveBatch(list,articleId);
		Map<String,Object> m = new HashMap<String,Object>();
		m.put("message", String.valueOf(1));

		return new ModelAndView(BaseUrl.message, m);
	}
	
	
	
	@RequestMapping(params = "action=saveColumns4Page")
	public ModelAndView saveColumns4Page(HttpServletRequest request) {
	
		int pageId = StringUtil.requestInt(request, "pageId");
		String[] catalogueId = request.getParameter("catalogueId").split(",");
		String[] horizontal = request.getParameter("horizontal").split(",");
		String[] row = request.getParameter("row").split(",");
		String[] orderNum = request.getParameter("orderNum").split(",");
		
		List<CmsPageRefColumnsBean>  list = new ArrayList<CmsPageRefColumnsBean>();
		for(int i=0;i<row.length;i++){
			CmsPageRefColumnsBean pc = new CmsPageRefColumnsBean();
			pc.setPageId(pageId);
			if(catalogueId[i].matches("\\d+"))
				pc.setCatalogueId(Integer.parseInt(catalogueId[i]));
			if(horizontal[i].matches("\\d+"))
				pc.setHorizontalOrder(Integer.parseInt(horizontal[i]));
			if(row[i].matches("\\d+"))
				pc.setRowOrder(Integer.parseInt(row[i]));
			if(orderNum[i].matches("\\d+"))
				pc.setOrderNum(Integer.parseInt(orderNum[i]));
			
			
			list.add(pc);
		}
		cmsPageRefColumnsService.saveBatchByPage(list,pageId);
		Map<String,Object> m = new HashMap<String,Object>();
		m.put("message", String.valueOf(1));

		return new ModelAndView(BaseUrl.message, m);
	}


	@RequestMapping(params = "action=savePage4Columns")
	public ModelAndView savePage4Columns(HttpServletRequest request) {
	
		int catalogueId = StringUtil.requestInt(request, "catalogueId");
		String[] pageId = request.getParameter("pageId").split(",");
		String[] horizontal = request.getParameter("horizontal").split(",");
		String[] row = request.getParameter("row").split(",");
		String[] orderNum = request.getParameter("orderNum").split(",");
		
		List<CmsPageRefColumnsBean>  list = new ArrayList<CmsPageRefColumnsBean>();
		for(int i=0;i<row.length;i++){
			CmsPageRefColumnsBean pc = new CmsPageRefColumnsBean();
			pc.setCatalogueId(catalogueId);
			if(pageId[i].matches("\\d+"))
				pc.setPageId(Integer.parseInt(pageId[i]));
			if(horizontal[i].matches("\\d+"))
				pc.setHorizontalOrder(Integer.parseInt(horizontal[i]));
			if(row[i].matches("\\d+"))
				pc.setRowOrder(Integer.parseInt(row[i]));
			if(orderNum[i].matches("\\d+"))
				pc.setOrderNum(Integer.parseInt(orderNum[i]));
			
			
			list.add(pc);
		}
		cmsPageRefColumnsService.saveBatchByColumns(list,catalogueId);
		Map<String,Object> m = new HashMap<String,Object>();
		m.put("message", String.valueOf(1));

		return new ModelAndView(BaseUrl.message, m);
	}
}
